<template>
	
	<view  :style="{paddingBottom:'20rpx' }">
		<view class="fixed">
			<!-- 选择日期 -->
			<uni-datetime-picker :show="showDate" type="date" v-model="selectDate" :end="endData" ref="dateTimePicker" @change="dateChange">
				<u-cell-group :border="false">
					<u-cell 
						rightIconStyle="width:24rpx;height:24rpx"
						titleStyle='font-size:28rpx;color:#3B3E45' :isLink="true" @click="selectDateClick">
						<template #title>
							<view class="u-slot-title">
								<text class="u-cell-text">选择日期：</text>
								<text class="u-cell-desc">
									{{selectDate ? selectDate :'请选择日期'}}
								</text>
							</view>
						</template>
					</u-cell>
				</u-cell-group>
			</uni-datetime-picker>
			
		</view>
		<view class="list">
			<view class="list-item" v-for="(newsitem,index) in formatlist" :key='index' >
				<view class="history-date">
					{{newsitem.sctime}}
				</view>
				<view class="list-data" v-for="(item,index) in newsitem.list" :key='index' @click="() =>toQyDetail(item)">
					<view class="jiancheng">
						{{item.companyname.slice(0,1)}}
					</view>
					<view class="productTitle">
						<view>{{item.companyname}}</view>
						<view class="tag grayTag blue">{{item.gongneng}}</view>
					</view>
				</view>
			</view>
		</view>
		<list-loading :status.sync="status" :listdata="historyList" :isBottom.sync="isScrollBottm" :defindNull="!historyList.length" >
			<view class="nullWrap">
				<image src="https://img.bidcenter.com.cn/uni/zanwushuju.png" mode="aspectFit"
					>
				</image>
			</view>
		</list-loading>
	</view>
</template>

<script>
	const d = new Date()
	const year = d.getFullYear()
	let month = d.getMonth() + 1
	month = month < 10 ? `0${month}` : month
	const date = d.getDate()
	export default {
		data() {
			return {
				location:8058,
				showDate: false,
				selectDate: '',
				endData:`${year}-${month}-${date}`,
				historyList: [],
				formatlist:[],
				title: '浙江卓宏建设项目管理有限公司关于绍兴市柯桥区实景三维数据建设',
				page:1,
				pageSize:20,
				isScrollBottm:false,
				status: 'loading',
				loadingText: '努力加载中...',
				// 自动加载，目前此状态用不到
				loadmoreText: '加载更多',
				nomoreText: '已全部加载完毕',
				isShowLoading:true,
			}
		},
		mounted() {
		},
		// 页面滚动到底部的事件（不是scroll-view滚到底），常用于下拉下一页数据
		
		methods: {
			scrollBottom() {
				this.isScrollBottm = true;
				if (this.historyList.length < 10) {
					return
				}
				if (this.status !== 'nomore') {
					this.getList({
							page: ++this.page
						},
						true
					);
				}
			},
			//打开日期组件
			selectDateClick() {
				this.$refs.dateTimePicker.show()
				this.showDate = true
			},
			// 点击日期组件确定按钮
			dateChange(e) {
				this.selectDate = e
				this.page = 1;
				this.showDate = false;
				this.historyList =[];
				this.formatlist = [];
				this.getList();
			},
			// 点击日期组件关闭按钮
			dateClose() {
				this.showDate = false
			},
			//跳转到企业信息
			toQyDetail(item){
				if (this.baseConfig.isIOSshenhe) {
					return
				}
				uni.navigateTo({
					url:'/pages/qiqing/index?companyName=' + item.companyname + '&comcode=' + item.comcode + '&vcode=' + item.vcode + '&type=' + 5
				})
				
			},
			
			// 是否显示 loading，初始化请求接口显示，滚动到底部加载下一页时显示A
			getList(params = {}, loadmore = false) {
				// 非下拉加载时
				if (!loadmore) {
					this.status = 'loading';
					this.nomoreText="努力加载中..."
					this.historyList = [];
					this.formatlist = [];
				}
				const paramsNew = Object.assign({}, {
						pageindex: this.page,
						pagesize: this.pageSize,
						location: this.location,
						starttime:this.selectDate,
						endtime: this.selectDate,
						backvcode:1
					},
					params
				);
				uni.$u.http
					.post('/User/log/GetCompanyFuWuShiYongListHandler.ashx', paramsNew)
					.then(rs => {
						const dt = rs.list || [];
						if (!dt.length) {
							this.status = 'nomore';
							this.nomoreText = loadmore ? '已全部加载完毕' : '暂无数据'
							this.isScrollBottm=false
							return;
						} else {
							if (dt.length < this.pageSize ) {
								this.status = 'nomore';
								this.nomoreText = '已全部加载完成'
							} else {
								this.status = 'loading';
							}
						}
						dt.map(item => {
							this.$set(item,'sctime',item.showtime.substring(0,10))
							this.historyList.push(item);		
						})
						this.historyList.forEach(item => {
						　　
						})
						this.formatlist = this.formattingData(this.historyList, 'sctime')
					})
					.catch(err => {
						this.status = 'nomore';
						// err.errMsg: 响应错误，如：404  err.msg：状态码retbs 非0
						uni.$u.toast(err.msg || '列表接口异常!');
					});
			},
			formattingData(arr, group_key) {
			// 先定义一个空对象和空数组；
			            let map = {};
			            let res = [];
			// 循环需要筛选的数组
			            for (let i = 0; i < arr.length; i++) {
			                let ai = arr[i];
			// 将需要筛选的属性的值作为新对象的键，并且判断是否已经存在
			                if (!map[ai[group_key]]) {
			// 不存在的话就在map对象中创建一个属性的值作为键名，键值为空数组的新对象，并且把arr[i]放入
			                    map[ai[group_key]] = [ai];
			                } else {
			// 如果已经存在就直接把arr[i]放入
			                    map[ai[group_key]].push(ai);
			                }
			            }
			// 循环后对map进行处理生成分组的数组
			            Object.keys(map).forEach((key) => {
			                res.push({
			                    [group_key]: key,
			                    list: map[key],
			                });
			            });
			
			            return res;
			 },
		},
	}
</script>

<style lang="scss" scoped>
	@import '@/static/css/liulanjilu.scss';
	.productTitle{
		flex: 1;
		margin-left: 20rpx;
	}
	.productTitle{
		font-weight: bold;
		font-size: 32rpx;
		.tag{
			font-weight: normal;
			display: inline-block;
		}
	    
	}
	.nullWrap{
		display: flex;
		justify-content: center;
		padding-top: 50rpx;
	}
	

	.pub-tab {
		width: 100%;
		background: #fff;
		margin-top: -2rpx;

		.tab-item {
			width: 50%;
			height: 96rpx;
			line-height: 96rpx;
			font-size: 32rpx;
			color: #3B3E45;
			text-align: center;
			position: relative;

			&::after {
				content: '';
				width: 0rpx;
				height: 6rpx;
				background: #2F6DFE;
				border-radius: 4rpx;
				position: absolute;
				left: 50%;
				bottom: 0rpx;
				margin-left: -29rpx;
				-webkit-transition: width 0.13s ease-in-out;
				transition: width 0.13s ease-in-out;
			}

			&.active {
				color: #2F6DFE;

				&::after {
					width: 58rpx;
				}
			}
		}
	}

	.fixed {
		width: 100%;
		position: sticky;
		top: 97rpx;
		left: 0;
		z-index: 99;
		background-color: #fff;
	}

	.list {
		// margin-top: 198rpx;
		padding-bottom: 20rpx;
		.list-item {
			margin:0 28rpx 0;
		}

		.list-data {
			padding:28rpx 24rpx 24rpx;
			background: #fff;
			border-radius: 10rpx;
			margin-top: 20rpx;
			display: flex;
			align-items: center;
			.jiancheng{
				width: 60rpx;
				height: 60rpx;
				background-color: #2F6DFE;
				color: #fff;
				font-size: 28rpx;
				text-align: center;
				border-radius: 5px;
				line-height: 60rpx;
			}
		}

		.title-box {
			.title {
				text {
					text-indent: 120rpx;
					line-height: 40rpx;
					font-size: 32rpx;
					color: #3B3E45;
					margin-left: 4rpx;
					font-weight: bold;
				}
			}
		}

		.infor-box {
			justify-content: space-between;
			margin-top: 2rpx;
			align-items: flex-end;
		}

		.time {
			margin-top: 10rpx;
			line-height: 40rpx;
			font-size: 22rpx;
			color: #6C717E;
		}
		.tag{
			margin-top: 10rpx;
		}
	}

	.history-date {
		height: 90rpx;
		line-height: 90rpx;
		font-size: 28rpx;
		color: #ADB4C2;
		margin-bottom: -20rpx;
	}

	.fujian {
		width: 24rpx;
		height: 24rpx;
	}
	.u-line{
		border-color: #eee !important;
	}
</style>
<style lang="scss">
	page {
		background: #FAFAFA;
	}
	
	.u-cell__body {
		padding: 30rpx !important;
		background: #fff;
	}
	
	.u-cell-text {
		font-size: 28rpx;
		color: #3B3E45;
	}
	
	.u-cell-desc {
		// margin-left: 8rpx;
		font-size: 28rpx;
		color: #ADB4C2;
	}
</style>